﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;

namespace Web
{
    public partial class Auditing : System.Web.UI.Page
    {
        private int editid = 0, page = 0;
        private static UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "QuestState"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["editid"] != null && Request.QueryString["editid"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["editid"]))
                {
                    editid = int.Parse(Request.QueryString["editid"]);
                }
                else
                {
                    Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
                }
            }
            else
            {
                Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
            }
            if (Request.QueryString["page"] != null && Request.QueryString["page"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    page = int.Parse(Request.QueryString["page"]);
                }
            }
            if (!IsPostBack)
            {
                LoadStatus(new List<int>());
                uinfo = UserValidate.GetInfo(this.Request);
                QuestionInfo qinfo = Question.GetQuestionInfoById(editid);
                //if (uinfo.Id == qinfo.PrinterId)
                //{
                //    Utility.ShowJsAlertAndCloseRefresh("您不能审核自己添加的试题", this);
                //}
                Title.Text = qinfo.Title;
                Serial.Text = qinfo.Serial;
                Score.Text = qinfo.Score.ToString();
                Answer.Text = qinfo.Answer;
                Printer.Text = qinfo.Printer;
                PrintDate.Text = qinfo.PrintDate.ToString();
                CreateDate.Text = qinfo.CreateDate.ToString();
                Remark.Text = qinfo.Remark;
                StatusList.SelectedValue = qinfo.StatusId.ToString();
                //if (qinfo.StatusId==4)
               // {
                    //在审核的时候对题目的详细信息的加载（所有情况下均加载）
                    //知识点
                    KnowledgeInfo kk =Knowledge.GetKnowledgeInfoById(qinfo.KnowId);
                    KnowledgePoint.Text = kk.Keyword ;

                    
                    //认知层次
                    CognitiveLevelInfo cc =CognitiveLevel.GetSingleInfo(qinfo.CognitiveLevelId);
                    KnowCeng.Text=cc.Keyword+"  "+cc.Name;
                    //难度级别
                    DifficInfo diff =Diffic.GetDifficInfoById(qinfo.DifficId);
                    Diff.Text=diff.Keyword +"  "+diff.Name;
                    //试题类型
                    QuesTypeInfo qt =QuesType.GetQuesTypeInfoById(qinfo.QuesTypeId);
                    QType.Text = qt.Keyword + "  " + qt.Name;
                    //Response.Write("<script language='javascript' type='text/javascript'> document.getElementById('daishenhe').style.display='block';</script>");
                //}
            }
        }


        protected void LoadStatus(IList<int> ids)
        {
            StatusList.Items.Clear();
            IList<StatusInfo> Status_Infos = new List<StatusInfo>();
            Status_Infos = Status.GetStatusInfos(0, 0);
            if (ids.Count > 0)
            {
                Status_Infos = Status_Infos.Where(t => GetArray<int>.Get(ids).Contains(t.Id)).ToList();
            }
            foreach (StatusInfo info in Status_Infos)
            {
                StatusList.Items.Add(new ListItem(info.Name, info.Id.ToString()));
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int _StatusID = 0;
            string _Remark = "";
            if (!PageValidate.IsNumberSign(StatusList.SelectedValue.Trim()))
            {
                Utility.ShowJsAlert("一般系统保护性错误", this);
                return;
            }
            else
            {
                _StatusID = int.Parse(StatusList.SelectedValue.Trim());
            }
            _Remark = Remark.Text.Trim();
            QuestionInfo qinfo = Question.GetQuestionInfoById(editid);
            if (UserValidate.CheckLogin(this.Request, "FirstTrial"))
            {
                if (qinfo.StatusId == 5)
                {
                    Utility.ShowJsAlert("您没有对已录用题目操作的权限", this);
                    return;
                }
                else if (qinfo.StatusId != 4 && qinfo.StatusId != 7)
                {
                    Utility.ShowJsAlert("您只能对需重审和待审核的题目进行操作", this);
                    return;
                }
                else
                {
                    if (_StatusID != 6 && _StatusID != 8 && _StatusID != 9)
                    {
                        Utility.ShowJsAlert("您只能将题目状态设为：被拒绝,已作废,已初审", this);
                        return;
                    }
                }
            }
            else if (UserValidate.CheckLogin(this.Request, "FinalInstance"))
            {
                if (qinfo.StatusId == 5)
                {
                    Utility.ShowJsAlert("您没有对已录用题目操作的权限", this);
                    return;
                }
                else if (qinfo.StatusId != 9)
                {
                    Utility.ShowJsAlert("您只能对已初审的题目进行操作", this);
                    return;
                }
                else
                {
                    if (_StatusID != 7 && _StatusID != 5)
                    {
                        Utility.ShowJsAlert("您只能将题目状态设为：已录用,需重审", this);
                        return;
                    }
                }
            }
            else if (!PageValidate.IsEmpty(uinfo.SubjectList))
            {
                if (("," + uinfo.SubjectList + ",").IndexOf("," + qinfo.SubjectId.ToString() + ",") < 1)
                {
                    Utility.ShowJsAlert("您不能审核这道题", this);
                    return;
                }
            }
            else if (!PageValidate.IsEmpty(uinfo.QuestionTypeList))
            {
                if (("," + uinfo.QuestionTypeList + ",").IndexOf("," + qinfo.QuesTypeId.ToString() + ",") < 1)
                {
                    Utility.ShowJsAlert("您不能审核这道题", this);
                    return;
                }
            }
            else if (!PageValidate.IsEmpty(uinfo.RoleList))
            {
                IList<int> UserIds = Users.GetUserIds(uinfo.RoleList);
                string UIds = "";
                if (UserIds.Count > 0)
                {
                    UIds = string.Join(",", ArrayHelper.ConvertToStringArray(GetArray<int>.Get(UserIds)));
                    if (("," + UIds + ",").IndexOf("," + qinfo.UserId.ToString() + ",") < 1)
                    {
                        Utility.ShowJsAlert("您不能审核这道题", this);
                        return;
                    }
                }

            }
            qinfo.StatusId = _StatusID;
            qinfo.State = (byte)_StatusID;
            if (_StatusID == 9)
            {
                qinfo.FirstReview = uinfo.Name;
            }
            else if (_StatusID == 5)
            {
                qinfo.FinalReview = uinfo.Name;
            }
            qinfo.Remark = _Remark;
            if (qinfo.Serial == "no")
            {
                string _SubjectKeyword = Subject.GetSubjectInfoById(qinfo.SubjectId).Keyword;
                //string _BookKeyword = Book.GetBookInfoById(qinfo.BookId).Keyword;
                string _KnowKeyword = Knowledge.GetKnowledgeInfoById(qinfo.KnowId).Keyword;
                string _QuesTypeKeyword = QuesType.GetQuesTypeInfoById(qinfo.QuesTypeId).Keyword;
                int count = Question.GetQuestionMax(new QuestionInfo { QuesTypeId = qinfo.QuesTypeId, KnowId = qinfo.KnowId }) + 1;
                qinfo.Serial = _SubjectKeyword + "-" + _KnowKeyword + "-" + _QuesTypeKeyword + "-" + count.ToString().PadLeft(4, Char.Parse("0"));
            }
            Question.EditQuestion(qinfo);
            Utility.ReloadTopPage(this);
        }

    }
}
